const app = getApp();
var sev = require('../../../../utils/sev.js');
const db = wx.cloud.database();
//直接上传一张图片
Page({
  data: {
    img: '',
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    hidden: true,
    //可以从Cloud获取比赛的详细信息
    list: [],
    listCur: 'A',
    modalName:null,
    rewardTypes:['国一','国二','国三','省一','省二','省三','校级','其他'],

    //
    cmpname:'',
    cmppub:'',
    cmprank:'',
    },
 radiock(e){
   console.log(e.currentTarget);
   wx.showLoading({
     title: '上传中',
     mask:true
   })
   this.setData({
     cmprank: e.currentTarget.dataset.value
   },()=>{
     //选择上传文件
     var timecode = sev.vcode(new Date());
     wx.chooseImage({
       count:1,
       sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
       sourceType: ['album', 'camera'], //从相册选择
       success: chooseResult => {
         // 将图片上传至云存储空间
         wx.cloud.uploadFile({
           // 指定上传到的云路径
           cloudPath: 'rewards/' + app.eduOS.xh  +timecode +'.png',
           // 指定要上传的文件的小程序临时文件路径
           filePath: chooseResult.tempFilePaths[0],
           // 成功回调
           success: res => {
             
             console.log('上传成功', res)
             wx.cloud.callFunction({
               name:'kindb',
               data:{
                 tablename:'rewardList',
                 opr:'add',
                 data:{
                   myxh:app.eduOS.xh,
                   cmpname: this.data.cmpname,
                   cmppub: this.data.cmppub,
                   cmprank: this.data.cmprank,
                   cloudid: res.fileID,
                   realurl: 'https://636c-cloud-18aa6f-1255645511.tcb.qcloud.la/rewards/' + app.eduOS.xh + timecode + '.png'
                 }
               }, success(tres) {
                 console.log('成功上传')
                 wx.hideLoading();
                 wx.navigateBack({
                   //出栈，返回上一层
                 })
               }, fail() {
                 console.log('上传失败')
                 wx.hideLoading();
               }
             })
             
             
           }
           
         })
       }, fail() {
         wx.hideLoading();
         wx.navigateBack({
           //失败返回上一页
         })
       }
     })
   })
 },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  sure(e){
    console.log(e.currentTarget);
    this.setData({
      cmpname: e.currentTarget.dataset.info.name,
      cmppub: e.currentTarget.dataset.info.support,
      modalName: 'RadioModal'
    })
    
  },
  onLoad() {
    var that = this;
    that.setData({
      img: app.globalData.imgList.indexesimg,
      list:app.globalData.rewards
    })
  },
  onReady() {
    let that = this;
    wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function(res) {
      that.setData({
        boxTop: res.top
      })
    }).exec();
    wx.createSelectorQuery().select('.indexes').boundingClientRect(function(res) {
      that.setData({
        barTop: res.top
      })
    }).exec()
  },
  //获取文字信息
  getCur(e) {
    //var that = this;
    this.setData({
      hidden: false,
      listCur: this.data.list[e.target.id].id,
    })
  },

  setCur(e) {
    console.log('set cur!')
    this.setData({
      hidden: true,
      listCur: this.data.listCur
    })
  },
  //滑动选择Item
  tMove(e) {
    let y = e.touches[0].clientY,
      offsettop = this.data.boxTop,
      that = this;
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      let num = parseInt((y - offsettop) / 20);
      this.setData({
        listCur: that.data.list[num].id
      })
    };
  },

  //触发全部开始选择
  tStart() {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd() {
    this.setData({
      hidden: true,
      listCurID: this.data.listCur
    })
  },
  indexSelect(e) {
    let that = this;
    let barHeight = this.data.barHeight;
    let list = this.data.list;
    let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
    for (let i = 0; i < list.length; i++) {
      if (scrollY < i + 1) {
        that.setData({
          listCur: list[i].id,
          movableY: i * 20
        })
        return false
      }
    }
  }
});